package com.pcbly.web.action.crm;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.crm.YkckBean;
import com.pcbly.web.business.crm.YkckService;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.form.AnalyseCommonDataExt;

/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/

public class YkckAction extends BaseAction<YkckBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9051987809011550924L;

	protected YkckService ykckService; 
	
	public void setYkckService(YkckService ykckService) {
		this.ykckService = ykckService;
	}
	private YkckBean ykck;
	
	private String orderBy;
	
	private Page<YkckBean> pages;
	
	private boolean asc = true;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public YkckBean getYkck() {
		return ykck;
	}
	public Page<YkckBean> getPages() {
		return pages;
	}

	public void setPages(Page<YkckBean> pages) {
		this.pages = pages;
	}
	public void setYkck(YkckBean ykck) {
		this.ykck = ykck;
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		ykck=ykckService.getYkckBeanById(getId());
		extendCustomer = ykckService.findYkckBeanExtView(
				"t_crm_ykck_ext", getId());
				
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		ykck=ykckService.getYkckBeanById(getId());
		extendCustomer = ykckService.findYkckBeanExtEdit(
				"t_crm_ykck_ext", getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}
	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();
		
		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<YkckBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = ykckService.findYkckBeanByCond(
				ykck, pages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	public String add() throws Exception { 
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		ykck=new YkckBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		extendCustomer = ykckService
				.findYkckBeanExtCreate("t_crm_ykck_ext");
		return super.add();
	}
	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(ykck.getId());
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_crm_ykck_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);
		
		try{
		if (ykck.getId() == null || "".equals(ykck.getId().trim())) {// new 
			ykck.setCreateDate(new Date());
			ykck.setCreateUserId(getUserId());
			ykck.setRecycled("0");
			ykck=ykckService.saveYkckBean(ykck);
			ext.setId(ykck.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				ykckService.saveYkckBeanExt(sql);
			}
			setMyMessage(getText("entity.saved"));
		} else {// update
			ykckService.updateYkckBean(ykck);
			
			boolean iflag = ykckService.getHasYkckBeanExtData(
					"t_crm_ykck_ext", ykck.getId());
			ext.setHaveData(iflag);
			ext.setId(ykck.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				ykckService.saveYkckBeanExt(sql);
			}
			
			setMyMessage(getText("entity.updated"));
		}}catch(Exception e){
			e.printStackTrace();
		}
		extendCustomer = ykckService.findYkckBeanExtEdit(
				"t_crm_ykck_ext", ykck.getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}
	public String dwrSave(YkckBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new 
			ykckService.saveYkckBean(bean);
		} else {// update
			ykckService.updateYkckBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}
	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if(getId().indexOf(",")>=0){
			ykckService.removeYkckBeanByIds(getId());
		}else{
			ykckService.removeYkckBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		ykck=null;
		return super.delete();
	}
	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if(id.indexOf(",")>=0){
			ykckService.removeYkckBeanByIds(id);
		}else{
			ykckService.removeYkckBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}
	
}